package com.bwei.topnews.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;

import com.bwei.topnews.R;
import com.bwei.topnews.adapter.CategoryAdapter;
import com.bwei.topnews.base.BaseData;
import com.bwei.topnews.base.BaseFragment;
import com.bwei.topnews.bean.NewsCategoryRoot;
import com.bwei.topnews.utils.CommonUtils;
import com.bwei.topnews.utils.URLUtils;
import com.bwei.topnews.xListView.XListView;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by qwe on 2016/11/1.
 */
public class MyFragment extends BaseFragment implements XListView.IXListViewListener {
    private XListView xListView;
    private int startIndex=0;
    private List<String> list = new ArrayList<>();
    private ArrayAdapter<String> stringArrayAdapter;
    private String category;
    private BaseData baseData;
    private CategoryAdapter adapter;
    private List<NewsCategoryRoot.DataBean> my_list = new ArrayList<>();

    @Override
    protected View initView(LayoutInflater inflater) {
        View view = inflater.inflate(R.layout.fragment,null);
        xListView = (XListView) view.findViewById(R.id.listView_Fragment);
        return view;
    }

   /* @Override
    public void onStart() {
        super.onStart();
        String str = CommonUtils.getSp("str");
        String[] split = str.split(",");
        for (int i = 0; i < split.length; i++) {
            my_list.get(i).setRead(true);
        }
    }*/

    @Override
    public void onStop() {
        super.onStop();
        adapter = null;
    }

    @Override
    protected void initActivityCreated( ) {
        category = this.getArguments().getString("title");
        Log.i("ddd", category);
        xListView.setPullLoadEnable(true);
        xListView.setXListViewListener(this);
        initData("");
        xListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String str = CommonUtils.getSp("str");
                String[] split = str.split(",");
                boolean flag = false;
                for (int i = 0; i < split.length; i++) {
                    if(split[i].equals(position+"")){
                        flag = true;
                    }
                }
                if(!flag){
                    String remethod = ","+position;
                    CommonUtils.saveSp("str",remethod);
                    my_list.get(position-1).setRead(true);
                    adapter.notifyDataSetChanged();
                }


            }
        });
    }
    private void showData(String data) {

        Gson gson =new Gson();
        NewsCategoryRoot newsCategoryRoot = gson.fromJson(data, NewsCategoryRoot.class);
        List<NewsCategoryRoot.DataBean> data1 = newsCategoryRoot.getData();
        my_list.addAll(0,data1);
        if(adapter==null) {
            adapter = new CategoryAdapter(my_list, getActivity());
            xListView.setAdapter(adapter);
        }else{
            adapter.notifyDataSetChanged();
        }


    }

    public String getTimeStamp(){
        return "&bd_loc_time=" + System.currentTimeMillis() / 1000;
    }
    public String getLoadTimeStamp() {
        return "&max_behot_time=" + System.currentTimeMillis() / 1000;
    }

    @Override
    public void onRefresh() {
        initData(getLoadTimeStamp());
        onLoad();
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onLoadMore() {

        initData(getLoadTimeStamp());
        onLoad();
        adapter.notifyDataSetChanged();
    }
    private void onLoad() {
        xListView.stopRefresh();
        xListView.stopLoadMore();
        xListView.setRefreshTime("刚刚");
    }
    private void initData(String requestPath) {
        baseData = new BaseData() {
            @Override
            public void setResultData(String data) {
                showData(data);
            }

            @Override
            public void setFailResult(int error_Net) {

            }
        };
        baseData.getData(URLUtils.URL_CATEGROY_PATH+category+requestPath,BaseData.LONGTTIME);
    }
    public static Fragment getFragment(String title) {
        MyFragment fragment = new MyFragment();
        Bundle bundle = new Bundle();
        bundle.putString("title", title);
        fragment.setArguments(bundle);
        return fragment;
    }
}
